package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.PagingViewHoder
import client.comm.baoding.R
import client.comm.baoding.api.bean.BuyCartRet
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.LayoutCateitemNew2Binding
import client.comm.baoding.databinding.LayoutCateitemNewBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.ui.GoodsDetailTradeActivity
import client.comm.commlib.base.BindingViewHoder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy

class RecommendAdapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<CateGoods.Goods>(object : DiffUtil.ItemCallback<CateGoods.Goods>() {
        override fun areItemsTheSame(
            oldItem: CateGoods.Goods,
            newItem: CateGoods.Goods
        ): Boolean {
            return oldItem.goods_id == newItem.goods_id
        }

        override fun areContentsTheSame(
            oldItem: CateGoods.Goods,
            newItem: CateGoods.Goods
        ): Boolean {
            return oldItem == newItem
        }
    }) {

    override fun getItemLayout(): Int {
        return R.layout.layout_cateitem_new2
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {
//        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = holder.binding as LayoutCateitemNew2Binding
        binding.event = this

        val bean = t as CateGoods.Goods
        manager.load(bean.goods_img)
//            .skipMemoryCache(true)
//            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(binding.img)

        if (bean.price > 0) {
            binding.tvPrice.text = "" + bean.price
            binding.priceShow = true

            if (bean.integral > 0) {
                binding.integral = "+" + bean.integral //+ "积分"
                binding.integralShow = true
            } else {
                binding.integral = ""
                binding.integralShow = false
            }
        } /*else if (bean.lpq_price > 0) {
            TextTool.getBuilder("${bean.lpq_price}")
                .setAlign(Layout.Alignment.ALIGN_CENTER).setTextSize(18)
                .append("体验券").setForegroundColor(Color.parseColor("#E81837"))
                .setTextSize(12)
                .into(binding.tvPrice)
            binding.priceShow = true

            if (bean.integral > 0) {
                binding.integral = "+" + bean.integral //+ "积分"
                binding.integralShow = true
            } else {
                binding.integral = ""
                binding.integralShow = false
            }

        }*/ else {
            binding.tvPrice.text = ""
            binding.priceShow = false

            if (bean.integral > 0) {
                binding.integral = "" + bean.integral //+ "积分"
                binding.integralShow = true
            } else {
                binding.integral = ""
                binding.integralShow = false
            }
        }
        binding.event = this
        binding.bean = bean
    }

    fun itemClick(bean: CateGoods.Goods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

}